import {
  type RouteRecordRaw,
  createRouter,
  createWebHistory,
} from "vue-router";
import Layouts from "@renderer/layouts/index.vue";
import Login from "@renderer/views/login/index.vue";

import Home from "@renderer/views/home/index.vue";
import { ref } from "vue";
import { ElMessage } from "element-plus";

export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/login",
    name: "Login",
    component: Login,
    meta: {
      hidden: true,
    },
  },
  {
    path: "/",
    redirect: "/home",
    component: Layouts,
    meta: {
      title: "layouts",
    },
    children: [
      {
        path: "/home",
        component: Home,
        meta: {
          hidden: true,
          title: "首页",
          location: ["header"],
        },
      },
      {
        path: "/purchasingManagement",
        redirect: "/purchasingManagement/overview",
        component: () => import("@renderer/views/productManagement/index.vue"),
        meta: {
          hidden: true,
          title: "采购管理",
          location: ["header"],
          layoutType: ["Aside", "Aside-Child"],
          menuTitle: "采购报表",
        },
        children: [
          {
            path: "/purchasingManagement/overview",
            component: () => import("@renderer/views/productManagement/overview.vue"),
            meta: {
              title: "采购总览",
              layoutType: "Aside",
            },
          },
          {
            path: "/purchasingManagement/supplierAdmin",
            component: () =>
              import("@renderer/views/productManagement/supplierAdmin.vue"),
            meta: {
              title: "供应商管理",
              layoutType: "Aside",
            },
          },
          {
            path: "/purchasingManagement/orderManagement",
            component: () =>
              import("@renderer/views/productManagement/orderManagement.vue"),
            meta: {
              title: "采购订单管理",
              layoutType: "Aside",
            },
          },
          {
            path: "/purchasingManagement/orderReview",
            component: () =>
              import("@renderer/views/productManagement/orderReview.vue"),
            meta: {
              title: "采购订单审核",
              layoutType: "Aside",
            },
          },
          {
            path: "/purchasingManagement/returns",
            component: () => import("@renderer/views/productManagement/returns.vue"),
            meta: {
              title: "采购退货",
              layoutType: "Aside",
            },
          },
          {
            path: "/purchasingManagement/returnReview",
            component: () =>
              import("@renderer/views/productManagement/returnReview.vue"),
            meta: {
              title: "采购退货审核",
              layoutType: "Aside",
            },
          },
          {
            path: "/purchasingManagement/detail",
            component: () => import("@renderer/views/productManagement/detail.vue"),
            meta: {
              title: "采购明细",
              layoutType: "Aside-Child",
            },
          },
          {
            path: "/purchasingManagement/statisticsProduct",
            component: () =>
              import("@renderer/views/productManagement/statisticsProduct.vue"),
            meta: {
              title: "采购统计 (按产品)",
              layoutType: "Aside-Child",
            },
          },
          {
            path: "/purchasingManagement/statisticalSuppliers",
            component: () =>
              import("@renderer/views/productManagement/statisticalSuppliers.vue"),
            meta: {
              title: "采购统计 (按供应商)",
              layoutType: "Aside-Child",
            },
          },
          {
            path: "/purchasingManagement/returnDetails",
            component: () =>
              import("@renderer/views/productManagement/returnDetails.vue"),
            meta: {
              title: "采购退货明细",
              layoutType: "Aside-Child",
            },
          },
          {
            path: "/purchasingManagement/returnStatisticsProducts",
            component: () =>
              import("@renderer/views/productManagement/returnStatisticsProducts.vue"),
            meta: {
              title: "退货统计 (按产品)",
              layoutType: "Aside-Child",
            },
          },
          {
            path: "/purchasingManagement/returnStatisticsSupplier",
            component: () =>
              import("@renderer/views/productManagement/returnStatisticsSupplier.vue"),
            meta: {
              title: "退货统计 (按供应商)",
              layoutType: "Aside-Child",
            },
          },
        ],
      },
      {
        path: "/salesManagement",
        component: () => import("@renderer/views/salesManagement/index.vue"),
        meta: {
          hidden: true,
          title: "销售管理",
          location: ["header"],
        },
      },
      {
        path: "/productManagement",
        component: () => import("@renderer/views/productManagement/index.vue"),
        meta: {
          hidden: true,
          title: "产品管理",
          location: ["header"],
        },
      },
      {
        path: "/warehouseManagement",
        component: () => import("@renderer/views/warehouseManagement/index.vue"),
        meta: {
          hidden: true,
          title: "仓储管理",
          location: ["header"],
        },
      },
      {
        path: "/financialManagement",
        component: () => import("@renderer/views/financialManagement/index.vue"),
        meta: {
          hidden: true,
          title: "财务管理",
          location: ["header"],
        },
      },
      {
        path: "/basicData",
        component: () => import("@renderer/views/basicData/index.vue"),
        meta: {
          hidden: true,
          title: "基础资料",
          location: ["header"],
        },
      },
      {
        path: "/systemSettings",
        redirect: "/systemSettings/organizationalStructure",
        component: () => import("@renderer/views/systemSettings/index.vue"),
        meta: {
          hidden: true,
          title: "系统设置",
          location: ["header"],
          layoutType: ["Aside", "Aside-Child"],
        },
        children: [
          {
            path: "/systemSettings/organizationalStructure",
            component: () =>
              import("@renderer/views/systemSettings/organizationalStructure.vue"),
            meta: {
              title: "组织架构",
              layoutType: "Aside",
            },
          },
          {
            path: "/systemSettings/rolePermissions",
            component: () =>
              import("@renderer/views/systemSettings/rolePermissions.vue"),
            meta: {
              title: "角色权限",
              layoutType: "Aside",
            },
          },
          {
            path: "/systemSettings/personnelManagement",
            component: () =>
              import("@renderer/views/systemSettings/personnelManagement.vue"),
            meta: {
              title: "人员管理",
              layoutType: "Aside",
            },
          },
          {
            path: "/systemSettings/userOperationLog",
            component: () =>
              import("@renderer/views/systemSettings/userOperationLog.vue"),
            meta: {
              title: "用户操作日志",
              layoutType: "Aside",
            },
          },
          {
            path: "/systemSettings/loginLog",
            component: () => import("@renderer/views/systemSettings/loginLog.vue"),
            meta: {
              title: "登录日志",
              layoutType: "Aside",
            },
          },
        ],
      },
    ],
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes: constantRoutes,
});

router.beforeEach((to, _from, next) => {
  const isLogin = ref(true);
  // isLogin.value = true;
  // 判断登录状态
  if (to.path !== "/login" && !isLogin.value) {
    ElMessage({
      showClose: true,
      message: "未登錄, 請先登錄",
      type: "error",
    });
    next("/login");
    return false;
  }

  next();
});

export default router;
